﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.ContentModel;
using Proggy.Web.Cms.Core.PropertyTypes;
using System.IO;
using Proggy.Web.Cms.Repositories.Published;
using Proggy.Web.Cms.Repositories.ContentModel;
using Proggy.Web.Cms.Service;
using Proggy.Web.Cms.Repositories.Membership;

namespace Proggy.Web.Cms.Core.ContentModels
{
    /// <summary>
    /// File content model type
    /// </summary>
    [ContentModel("File", "~/Areas/ProggyCore/Content/Images/ContentModelIcons/File.png", false,false,  null, "N/a", "Proggy.Web.Cms.Core.Controllers.ContentModel")]
    public class ProggyFileContentModel : ProggyCoreContentModelBase, IContentModel
    {
        public ProggyFileContentModel(IContentModelTypeRepository cmRepo,  IMembershipRepository membRepo) : base(cmRepo, membRepo) { }     
        public ProggyFileContentModel() : base() { }

        /// <summary>
        /// The file property
        /// </summary>
        [ContentModelPropertyType("File", "File", "File")]
        public ProggyFileUploader File { get; set; }

        /// <summary>
        /// After model binding
        /// </summary>
        /// <param name="repo"></param>
        public override void OnAfterModelBinding(IContentCrudService repo)
        {
            // set file properties for posted file
            if (this.File.PostedFile != null)
            {
                // get server path
                var baseFileStoreServerPath = AppDomain.CurrentDomain.BaseDirectory + @"\Content\ProggyFileStore";
                var thisModelFileStorePath = baseFileStoreServerPath + @"\" + this.Id;

                // set basic properties
                this.File.MimeType = this.File.PostedFile.ContentType;
                this.File.FileName = this.File.PostedFile.FileName;
                this.File.FilePath = "~/Content/ProggyFileStore/" + this.Id + "/" + this.File.FileName;
                this.File.ServerFilePath = thisModelFileStorePath + @"\" +this.File.FileName;

                // see if root folder exists
                if (!Directory.Exists(baseFileStoreServerPath))
                {
                    // create
                    Directory.CreateDirectory(baseFileStoreServerPath);
                }

                // see if this folder exists
                if (!Directory.Exists(thisModelFileStorePath))
                {
                    // create
                    Directory.CreateDirectory(thisModelFileStorePath);
                }

                // save
                this.File.PostedFile.SaveAs(this.File.ServerFilePath);
            }
            else
            {
                // check
                if (
                    string.IsNullOrEmpty(this.File.MimeType) ||
                    string.IsNullOrEmpty(this.File.FilePath) ||
                    string.IsNullOrEmpty(this.File.FileName) ||
                    string.IsNullOrEmpty(this.File.ServerFilePath)
                )
                {

                    // error
                    throw new Exception("A file editor for this content has no file. Please check.");

                }

            }
        }

        /// <summary>
        /// After publish
        /// </summary>
        /// <param name="repo"></param>
        /// <param name="contentModel"></param>
        public override void OnAfterSuccessfulPublish(IContentCrudService repo)
        {
            // get paths
            var baseFileStoreServerPath = AppDomain.CurrentDomain.BaseDirectory + @"\Content\ProggyFileStore";
            var thisModelFileStorePath = baseFileStoreServerPath + @"\" + this.Id;

            // get folder
            var folder = Directory.GetFiles(thisModelFileStorePath);

            // check
            foreach (var file in folder)
            {
                // check against current
                if (file != this.File.ServerFilePath)
                {
                    System.IO.File.Delete(file);
                }
            }
        }
    }
}
